﻿var PCList_Select_MAC = "";
var PCList_Select_UserName = "";

function PCList_MakeItem(item,online)
{
	// "hostname":"VEDA","mac":"40-16-7E-0D-3E-83","starttime":"2017-06-13 23:39:58","endtime":"2017-06-13 23:41:28"
	var result = "";
	if(PCList_Select_MAC == item.mac){
		result ="<li class='PCLiveListItem' id="+item.mac+">\
				<div class='uk-card uk-card-default' style='pointer-events:none;width:100%;background-color:#00CC66'>\
					<table style='pointer-events:none;min-height:60px;width:100%'>\
						<tr>\
							<td width='25px'><img src='/img/"+online+".png'/></td>\
							<td style='display:none' class='show'><span>主机名:&nbsp&nbsp"+item.hostname+"</span></td>\
							<td class='hide'><span>主机名:&nbsp&nbsp"+ item.hostname+"</span><br><span>MAC地址:&nbsp"+item.mac+"</span><br><span>开机时间: "+item.starttime+"</span></td>\
						</tr>\
					</table>\
				</div>\
			</li>"
	}
	else{		
		result ="<li class='PCLiveListItem' id="+item.mac+">\
				<div class='uk-card uk-card-default' style='pointer-events:none;width:100%;'>\
					<table style='pointer-events:none;min-height:60px;width:100%'>\
						<tr>\
							<td width='25px'><img src='/img/"+online+".png'/></td>\
							<td class='show'><span>主机名:&nbsp&nbsp"+item.hostname+"</span></td>\
							<td style='display:none' class='hide'><span>主机名:&nbsp&nbsp"+ item.hostname+"</span><br><span>MAC地址:&nbsp"+item.mac+"</span><br><span>开机时间: "+item.starttime+"</span></td>\
						</tr>\
					</table>\
				</div>\
			</li>"
	}
		
	return result;	
}

function PCList_Update()
{
	var info = "http://121.40.16.218:7789/PCLive/GetPCLiveList?username="+PCList_Select_UserName+"&jsoncallback=?";
	$.getJSON(info,
	function(data){
	  var result ="";
	  // 得到了在线列表
	  if(data.status == "success"){		  
		for(var item in data.online) {
			result += PCList_MakeItem(data.online[item], "Online");
		}
		for(var item in data.offline) {
			result += PCList_MakeItem(data.offline[item], "Offline");
		}
	  }
	  
	  	$("#PCLiveList").html(result);
		$(".PCLiveListItem").click(PCList_ItemClick);	
	});
}

function PCList_ItemClick(e)
{
	// 显示自己的内容并通知影藏其他同伴的内容
	$(e.target).find(".hide").show();
	$(e.target).find(".show").hide();
	$(e.target).find(".uk-card").css({"background-color":"#00CC66"});
	PCList_Select_MAC = $(e.target).attr("id");
	
	$(e.target).siblings().find(".hide").hide();
	$(e.target).siblings().find(".show").show();
	$(e.target).siblings().find(".uk-card").css({"background-color":"#ffffff"});
	
	Calendar_UpdateSummary();
}

function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
	return unescape(arr[2]);
	else
	return null;
}
	
$(function()
{
	PCList_Select_UserName = getCookie("PCLiveUserNmae");
	if(PCList_Select_UserName == null){
		location.href = "http://121.40.16.218:7789";
	}
	setInterval(PCList_Update, 5000);
	PCList_Update();
});